package com.demo.convert.html2pdf;

import com.demo.convert.exception.Html2PdfException;
import com.demo.convert.tool.FileTool;
import com.itextpdf.text.pdf.BaseFont;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xhtmlrenderer.resource.XMLResource;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author fenggang.li
 * @create 2019-09-12 11:22
 */
public class Html2PdfConverter {
    /**
     * Convert html to pdf.
     *
     * @param htmlFilePath
     * @param pdfFilePath
     * @throws Html2PdfException
     */
    public static void convert(String htmlFilePath, String pdfFilePath) throws Html2PdfException {
        FileInputStream fileInputStream = null;
        FileOutputStream pdfOutputStream = null;
        try {
            fileInputStream = new FileInputStream(htmlFilePath);
        } catch (FileNotFoundException e) {
            throw new Html2PdfException("Html file not found. " + htmlFilePath);
        }
        try {
            pdfOutputStream = new FileOutputStream(pdfFilePath);
        } catch (FileNotFoundException e) {
            throw new Html2PdfException("Folder of pdf file not found. " + htmlFilePath);
        }

        convert(htmlFilePath, fileInputStream, pdfOutputStream);
    }

    /**
     * Convert html to pdf.
     *
     * @param htmlBaseFolder Used to locate the relative resources such as images in the html file.
     * @param htmlInputStream
     * @param pdfOutputStream
     * @throws Html2PdfException
     */
    public static void convert(String htmlBaseFolder, InputStream htmlInputStream, OutputStream pdfOutputStream) throws Html2PdfException {
        ITextRenderer pdfRenderer = new ITextRenderer();
        /** source */
        Document htmlDocument = XMLResource.load(htmlInputStream).getDocument();
        String htmlBaseUrl = null;
        try {
            htmlBaseUrl = FileTool.toUrl(htmlBaseFolder);
        } catch (MalformedURLException e) {
            throw new Html2PdfException("Malformed html base url.", e);
        }
        pdfRenderer.setDocument(htmlDocument, htmlBaseUrl);

        /** config */
        pdfRenderer.setScaleToFit(true);
        // fonts
        addFonts(pdfRenderer);

        /** convert */
        pdfRenderer.layout();
        try {
            pdfRenderer.createPDF(pdfOutputStream, true);
        } catch (Exception e) {
            throw new Html2PdfException("Convert error", e);
        }
    }

    public static void addFonts(ITextRenderer pdfRenderer) throws Html2PdfException {
        ITextFontResolver fontResolver = pdfRenderer.getFontResolver();
        try {
            URL resource = Html2PdfConverter.class.getResource("/fonts/simsun.ttf");
            String fontPath = resource.getPath();
            // 添加默认的中文依赖SumSun
            fontResolver.addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        } catch (Exception e) {
            throw new Html2PdfException("Add font error.", e);
        }
    }
}
